package com.frejoys.app.controller;

import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.app.UserCodeDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.app.RecommendCount;
import com.frejoys.common.vo.app.UserLevelVo;
import com.frejoys.service.CommonService;
import com.frejoys.service.UserCodeService;
import com.frejoys.service.UserLevelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "用户关联", description = "用户关联")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixApp)
@Authorization(Authority.Private)
public class UserLevelController {

    private final UserLevelService userLevelService;

    private final RedissonClient redissonClient;

    private final CommonService commonService;

    private final UserCodeService userCodeService;

    @Operation(summary = "扫码关联")
    @PostMapping("/userLevel/add")
    public Result userLevelAdd(@RequestBody @Validated UserCodeDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.userExtendLockKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                Long id = userCodeService.codeByUserId(dto.getCode());
                userLevelService.addUserLevel(id, RequestUtil.userId());

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }


    @PostMapping("/userLevel/oneUserInfo")
    @Operation(summary = "用户推广一级信息列表")
    public Result<List<UserLevelVo>> userLevelOneUserInfo(@RequestBody @Validated PageDto dto){
        return Result.success(userLevelService.oneUserLevelInfo(RequestUtil.userId(),dto));
    }


    @PostMapping("/userLevel/twoUserInfo")
    @Operation(summary = "用户推广二级信息列表")
    public Result<List<UserLevelVo>> userLevelTwoUserInfo(@RequestBody @Validated PageDto dto){
        return Result.success(userLevelService.twoUserLevelInfo(RequestUtil.userId(),dto));
    }


    @PostMapping("/userLevel/myReference")
    @Operation(summary = "我的推荐人")
    public Result<UserLevelVo> myReference(){
        return Result.success(userLevelService.myReference(RequestUtil.userId()));
    }

    @PostMapping("/userLevel/recommendCount")
    @Operation(summary = "我的推荐人数")
    public Result<RecommendCount> recommendCount(){
        return Result.success(userLevelService.recommendCount(RequestUtil.userId()));
    }
}
